<?php
$filename  = dirname(__FILE__).'/data.txt';
$msg = isset($_GET['msg']) ? $_GET['msg'] : '';
if ($msg != '')
{
	//写入内容至文件
	file_put_contents($filename,$msg);
	die();
}
set_time_limit(0);
$lastmodif    = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;

//取得文件最后修改时间
$currentmodif = filemtime($filename);

while ($currentmodif <= $lastmodif) 
{	
	//有释放CPU占用率的作用
  	usleep(10000); 
  	//清除文件缓存信息
  	clearstatcache();
  	$currentmodif = filemtime($filename);
}

// return a json array
$response = array();
$response['msg']       = file_get_contents($filename);
$response['timestamp'] = $currentmodif;
echo json_encode($response);
ob_flush();
flush();
?>